package com.woniuxy.tfss.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.woniuxy.tfss.entity.Rider;
import com.woniuxy.tfss.exception.NoAccountException;
import com.woniuxy.tfss.exception.PasswordWrongException;
import com.woniuxy.tfss.exception.RiderExistException;
import com.woniuxy.tfss.mapper.RiderMapper;
import com.woniuxy.tfss.service.RiderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.List;

@Service
public class RiderServiceImpl extends ServiceImpl<RiderMapper, Rider> implements RiderService {
    @Autowired
    private RiderMapper riderMapper;

    @Override
    public Rider getByPhoneNum(String phoneNum) {
        return riderMapper.getByPhoneNum(phoneNum);
    }

    @Override
    public Rider login(String phoneNum, String password) {
        QueryWrapper<Rider> wraaper1 = new QueryWrapper<>();
        wraaper1.eq("phonenum", phoneNum);
        QueryWrapper<Rider> wraaper2 = new QueryWrapper<>();
        wraaper2.eq("phonenum",phoneNum);
        wraaper2.eq("password",password);
        if (riderMapper.selectCount(wraaper1) == 0) {
            throw new NoAccountException("账号不存在！");
        }else{
            if(riderMapper.selectCount(wraaper2)==0){
                throw new PasswordWrongException("密码错误！");
            }else{
                return riderMapper.selectOne(wraaper2);
            }
        }

    }

    @Override
    public void reg(Rider rider) {
        QueryWrapper<Rider> wrapper1 = new QueryWrapper<>();
        wrapper1.eq("phonenum", rider.getPhoneNum());
        QueryWrapper<Rider> wrapper2 = new QueryWrapper<>();
        wrapper2.eq("identity", rider.getIdentity());
        if (riderMapper.selectCount(wrapper1)!=0){
            throw  new NoAccountException("账号已存在");
        }else if (riderMapper.selectCount(wrapper2)!=0){
            throw new RiderExistException("骑手已注册");
        }else {
            rider.setCredit(3.0);
            rider.setProfit(new BigDecimal(0));
            rider.setWorkStatus(Rider.NORMAL);
            rider.setStatus(Rider.NORMAL);
            riderMapper.insert(rider);
        }
    }

    @Override
    public List<Rider> getWaitRiders() {
        return riderMapper.getWaitRiders();
    }

}
